package com.example.poemtest.index.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;

import androidx.annotation.RequiresApi;

import com.example.poemtest.R;
import com.example.poemtest.entity.StudyPoetryBean;
import com.example.poemtest.index.activity.StudyPoetryDetailActivity;
import com.example.poemtest.index.entity.Poetry;

import java.util.ArrayList;
import java.util.List;

/**
 * 小初高左侧主页面ListView的适配器
 * created by JHT on 2024/3/25
 */
public class PoetryAdapter extends BaseAdapter implements PoetryItemAdapter.InnerItemOnclickListener,
        AdapterView.OnItemClickListener {
    private Context context;
    private List<StudyPoetryBean.StudyPoetry> poetryDatas;
    ViewHold viewHold = null;
    List<StudyPoetryBean.StudyPoetry> dataList = new ArrayList<>();

    public PoetryAdapter(Context context, List<StudyPoetryBean.StudyPoetry> poetryDatas) {
        this.context = context;
        this.poetryDatas = poetryDatas;
    }

    @Override
    public int getCount() {
        if (poetryDatas != null) {
            return poetryDatas.size();
        } else {
            return 10;
        }
    }

    @Override
    public Object getItem(int position) {
        return poetryDatas.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        StudyPoetryBean.StudyPoetry dataBean = poetryDatas.get(position);

        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_study, null);
            viewHold = new ViewHold();
            viewHold.listView = convertView.findViewById(R.id.listView);
            convertView.setTag(viewHold);
        } else {
            viewHold = (ViewHold) convertView.getTag();
        }
        PoetryItemAdapter adapter = new PoetryItemAdapter(context, poetryDatas);
        adapter.setOnInnerItemOnClickListener(this);
        viewHold.listView.setAdapter(adapter);
        viewHold.listView.setOnItemClickListener(this);
        return convertView;
    }

    //listView的item点击事件
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //点击每一个item之后进行跳转，传递诗词的信息
        Poetry poetry = new Poetry();
//        Log.e("名字",dataList.get(position).getPoetry_name().toString());
        poetry.setPoetry_name(dataList.get(position).getPoetryName());
        poetry.setPoetry_author(dataList.get(position).getPoetryAuthor());
        poetry.setPoetry_author_dynasty(dataList.get(position).getPoetryAuthorDynasty());
        poetry.setPoetry_content(dataList.get(position).getPoetryContent());
        poetry.setPoetry_desc(dataList.get(position).getPoetryDesc());
//        Log.e("1名字",poetry.getPoetry_name());
        Intent intent = new Intent(context, StudyPoetryDetailActivity.class);
        intent.putExtra("poetry", poetry);
        Log.e("诗词",poetry.toString());
        context.startActivity(intent);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void itemClick(View v) {
        int position;
        position = (Integer) v.getTag();
        switch (v.getId()) {

        }

//        //获取用户选择的食物的热量信息。
//        for(StudyPoetryBean.DataBean.DataListBean bean : dataList) {
//
//        }

    }


    //listView的item点击事件
//    @Override
//    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//
//    }

    private static class ViewHold {
        private ListView listView;
    }
}
